package com.atguigu.flink.chapter07.state;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.state.ListState;
import org.apache.flink.api.common.state.ListStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.state.FunctionInitializationContext;
import org.apache.flink.runtime.state.FunctionSnapshotContext;
import org.apache.flink.streaming.api.checkpoint.CheckpointedFunction;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

import java.util.ArrayList;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2022/1/18 9:19
 */
public class Flink01_State_Operator_List {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);
        
        env.enableCheckpointing(1000);// checkpoint周期
        
        env
            .socketTextStream("hadoop162", 9999)
            .flatMap(new MyFlatMapFunction())
            .print();
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static class MyFlatMapFunction implements FlatMapFunction<String, String>, CheckpointedFunction {
        ArrayList<String> words = new ArrayList<>();  // 每个并行度一个list集合
        private ListState<String> wordsState;
    
        @Override
        public void flatMap(String line, Collector<String> out) throws Exception {
            // 手动抛出一个异常, 程序因为开了checkpoint, 会自动重启
            if (line.contains("x")) {
                throw new RuntimeException("手动抛异常, flink自动重启");
            }
            
            // 把每一行字符串,切成一个个的单词, 放入到一个list集合中,就是我们需要保存的状态
            for (String word : line.split(",")) {
                words.add(word);
            }
            out.collect(words.toString());
        }
        // 给状态做快照, 把状态持久化, 将来恢复的时候可以从持久化的位置实现恢复状态中的数据
        // 周期性的保存状态, 有车checkpoint的周期决定. 每个并行执行一次
        
        @Override
        public void snapshotState(FunctionSnapshotContext ctx) throws Exception {
//            System.out.println("MyFlatMapFunction.snapshotState");
            wordsState.update(words);  // 用新集合中的元素, 覆盖状态中的元素
            
           /* wordsState.clear();
            wordsState.addAll(words);*/
            
        }
        
        // 初始化状态: 当程序启动的时候, 会对状态进行恢复
        // 当程序启动的时候执行, 有几个并行度就执行几次, 每个并行度都要恢复自己的状态
        @Override
        public void initializeState(FunctionInitializationContext  ctx) throws Exception {
            System.out.println("MyFlatMapFunction.initializeState");
            // 从checkpoint中获取保存的算子状态, 每个并行度一份
            wordsState = ctx.getOperatorStateStore().getListState(new ListStateDescriptor<String>("ws", String.class));
    
            Iterable<String> it = wordsState.get();
            for (String word : it) {
                words.add(word);
            }
        }
    }
}
